package com.edu.hre.db.sql.admin;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import com.edu.hre.common.utils.CommonUtils;
import com.edu.hre.db.pojo.HreNationality;
import com.edu.hre.db.service.BaseService;
import com.edu.hre.db.utils.Compare;
import com.edu.hre.struts.form.admin.NationalityForm;
import com.edu.hre.struts.form.admin.NationalityListForm;

public class NationalityDAOService implements Serializable {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 3394301416150421253L;
	public static final String BEAN_NAME = "nationalityDAOService";
	private BaseService baseService  = null;

	public void setBaseService(BaseService baseService) {
		this.baseService = baseService;
	} 

	public ArrayList<NationalityForm> getNationalityListInfo(int limit, int offset){

		List<HreNationality> hreNationalities = baseService.getPojos(null, -1, null, new String[]{"dateCreated"}, new Boolean[]{true}, limit, offset, HreNationality.class);
		ArrayList<NationalityForm> nationalities = new ArrayList<NationalityForm>();
		
		if (hreNationalities == null) {
			return nationalities;
		}
		for (HreNationality hreEdu: hreNationalities) {

			try {

				NationalityForm nationality = new NationalityForm();
				nationality.setId(hreEdu.getId() + "");
				nationality.setName(CommonUtils.normalizeString(hreEdu.getName()));

				nationalities.add(nationality);
			} catch (Exception e) {
				CommonUtils.println(e.getMessage());
			}
		}
		
		return nationalities;
	}
	
	public int countNationalities(){

		int count = baseService.countByColumns(null, null, null, "id", HreNationality.class);
		return count < 0 ? 0 : count;
	}

	public void deleteNationalities(NationalityListForm form){

		ArrayList<NationalityForm> info = (ArrayList<NationalityForm>) form.getAllNationalities();

		for (int i = 0; i < info.size(); i++) {

			try {

				NationalityForm nationality = info.get(i);
				if (nationality.isSelected()) {
					baseService.deleteFirstPojo("id", Compare.EQUAL, Integer.parseInt(nationality.getId()), null, null, 0, HreNationality.class);
				}
			} catch (Exception e) {
				CommonUtils.println(e.getMessage());
			}
		}
	}
}
